﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantasyBlog.DAL;

namespace FantasyBlog.BLL
{
    public class PostProvider:IPostProvider
    {
        public IQueryable<DAL.Post> GetPosts()
        {
            var posts = (from p in Model.PostSet
                         orderby p.date descending
                         select p);
            return posts;
        }

        public IQueryable<DAL.Post> GetTagPosts(string tag)
        {
            var posts = (from p in Model.PostSet
                         where p.Terms.Contains((
                             from t in Model.TermSet
                             where t.name == tag 
                             select t).FirstOrDefault())
                         orderby p.date descending
                         select p);
            return posts.AsQueryable();
        }

        public IQueryable<DAL.Post> GetPostByUser(string username)
        {
            var posts = (from p in Model.PostSet
                         where p.PostUser.username == username
                         select p);
            return posts;
        }

        public IQueryable<DAL.Post> GetPostByDate(DateTime starttime, DateTime endtime)
        {
            var posts = (from p in Model.PostSet
                         where p.date >= starttime && p.date <= endtime
                         select p);
            return posts;
        }

        public IQueryable<DAL.Post> SearhPost(string Keyword)
        {
            var posts = (from p in Model.PostSet
                         where p.title.Contains(Keyword)
                         select p);
            return posts;
        }

        public Post GetPost(int id)
        {
            return Model.PostSet.Find(id);
        }

        public IQueryable<Post> GetPostsByCategory(string Name)
        {
            var posts = (from p in Model.PostSet
                         where p.Category.name == Name
                         orderby p.date descending
                         select p);
            return posts;
        }

        private BlogModelContainer Model = new BlogModelContainer();

    }
}
